<?php

namespace app\store\service;
use think\facade\Db;

class UserService
{   
    public function getUserList($param)
    {
        $store_id = session('store_id');
        $query = Db::name('user')
                    ->field('c.id,a.avatar,a.nickname,a.gender,a.id,a.is_agent,a.type,b.grade_name,c.register_time')
                    ->alias('a')
                    ->withAttr('gender',function ($value){
                        $gender = ['未知','男','女'];
                        return $gender[$value];
                    })->join('user_grade b','a.user_grade_id=b.id')
                    ->join('user_auth c','a.user_id=c.id')
                    ->where('a.store_id',$store_id);


        if(!empty($param['keyword'])){

            $query->where('a.nickname',$param['keyword']);
        }

        $StoreUsers = $query->order('c.register_time','desc')->paginate(10);
        return $StoreUsers;
    }

    public function findUser($id)
    {   
        $storeUser = Db::name('user')
                        ->alias('a')
                        ->field('c.id,a.avatar,a.nickname,a.gender,a.birthday,a.integral,a.gold,a.balance,
                        a.user_grade_id,b.grade_name,c.phone,c.register_time')
                        ->join('user_grade b','a.user_grade_id=b.id')
                        ->join('user_auth c','a.user_id=c.id')
                        ->withAttr('gender',function ($value){
                            $gender = ['未知','男','女'];
                            return $gender[$value];
                        })->withAttr('register_time',function ($value){
                            return date('Y-m-d H:i:s',$value);
                        })->where('a.user_id',$id)
                        ->find();

        return $storeUser;
    }
}